<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Eigen Interoperability</title>
<link rel="stylesheet" href="../../multiprecision.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Chapter 1. Boost.Multiprecision">
<link rel="up" href="../tut.html" title="Tutorial">
<link rel="prev" href="hash.html" title="Hash Function Support">
<link rel="next" href="new_backend.html" title="Writing a New Backend">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="hash.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tut.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="new_backend.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_multiprecision.tut.eigen"></a><a class="link" href="eigen.html" title="Eigen Interoperability">Eigen Interoperability</a>
</h3></div></div></div>
<p>
        This library provides the header:
      </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">multiprecision</span><span class="special">/</span><span class="identifier">eigen</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
</pre>
<p>
        which defines the traits classes and functions that the Eigen library needs
        all user-defined number types to provide.
      </p>
<p>
        For example the following code performs quad-precision matrix solving on
        complex numbers:
      </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">multiprecision</span><span class="special">/</span><span class="identifier">cpp_complex</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">multiprecision</span><span class="special">/</span><span class="identifier">eigen</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">Eigen</span><span class="special">/</span><span class="identifier">Dense</span><span class="special">&gt;</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
<span class="special">{</span>
   <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">Eigen</span><span class="special">;</span>
   <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">cpp_complex_quad</span> <span class="identifier">complex_type</span><span class="special">;</span>
   <span class="comment">//</span>
   <span class="comment">// We want to solve Ax = b for x,</span>
   <span class="comment">// define A and b first:</span>
   <span class="comment">//</span>
   <span class="identifier">Matrix</span><span class="special">&lt;</span><span class="identifier">complex_type</span><span class="special">,</span> <span class="number">2</span><span class="special">,</span> <span class="number">2</span><span class="special">&gt;</span> <span class="identifier">A</span><span class="special">,</span> <span class="identifier">b</span><span class="special">;</span>
   <span class="identifier">A</span> <span class="special">&lt;&lt;</span> <span class="identifier">complex_type</span><span class="special">(</span><span class="number">2</span><span class="special">,</span> <span class="number">3</span><span class="special">),</span> <span class="identifier">complex_type</span><span class="special">(-</span><span class="number">1</span><span class="special">,</span> <span class="special">-</span><span class="number">2</span><span class="special">),</span> <span class="identifier">complex_type</span><span class="special">(-</span><span class="number">1</span><span class="special">,</span> <span class="special">-</span><span class="number">4</span><span class="special">),</span> <span class="identifier">complex_type</span><span class="special">(</span><span class="number">3</span><span class="special">,</span> <span class="number">6</span><span class="special">);</span>
   <span class="identifier">b</span> <span class="special">&lt;&lt;</span> <span class="number">1</span><span class="special">,</span> <span class="number">2</span><span class="special">,</span> <span class="number">3</span><span class="special">,</span> <span class="number">1</span><span class="special">;</span>
   <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Here is the matrix A:\n"</span> <span class="special">&lt;&lt;</span> <span class="identifier">A</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
   <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Here is the right hand side b:\n"</span> <span class="special">&lt;&lt;</span> <span class="identifier">b</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
   <span class="comment">//</span>
   <span class="comment">// Solve for x:</span>
   <span class="comment">//</span>
   <span class="identifier">Matrix</span><span class="special">&lt;</span><span class="identifier">complex_type</span><span class="special">,</span> <span class="number">2</span><span class="special">,</span> <span class="number">2</span><span class="special">&gt;</span> <span class="identifier">x</span> <span class="special">=</span> <span class="identifier">A</span><span class="special">.</span><span class="identifier">fullPivHouseholderQr</span><span class="special">().</span><span class="identifier">solve</span><span class="special">(</span><span class="identifier">b</span><span class="special">);</span>
   <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"The solution is:\n"</span> <span class="special">&lt;&lt;</span> <span class="identifier">x</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
   <span class="comment">//</span>
   <span class="comment">// Compute the error in the solution by using the norms of Ax - b and b:</span>
   <span class="comment">//</span>
   <span class="identifier">complex_type</span><span class="special">::</span><span class="identifier">value_type</span> <span class="identifier">relative_error</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">A</span><span class="special">*</span><span class="identifier">x</span> <span class="special">-</span> <span class="identifier">b</span><span class="special">).</span><span class="identifier">norm</span><span class="special">()</span> <span class="special">/</span> <span class="identifier">b</span><span class="special">.</span><span class="identifier">norm</span><span class="special">();</span>
   <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"The relative error is: "</span> <span class="special">&lt;&lt;</span> <span class="identifier">relative_error</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
   <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
        Which produces the following output:
      </p>
<pre class="programlisting"><span class="identifier">Here</span> <span class="identifier">is</span> <span class="identifier">the</span> <span class="identifier">matrix</span> <span class="identifier">A</span><span class="special">:</span>
<span class="special">(</span><span class="number">2</span><span class="special">,</span><span class="number">3</span><span class="special">)</span> <span class="special">(-</span><span class="number">1</span><span class="special">,-</span><span class="number">2</span><span class="special">)</span>
<span class="special">(-</span><span class="number">1</span><span class="special">,-</span><span class="number">4</span><span class="special">)</span>   <span class="special">(</span><span class="number">3</span><span class="special">,</span><span class="number">6</span><span class="special">)</span>
<span class="identifier">Here</span> <span class="identifier">is</span> <span class="identifier">the</span> <span class="identifier">right</span> <span class="identifier">hand</span> <span class="identifier">side</span> <span class="identifier">b</span><span class="special">:</span>
<span class="number">1</span> <span class="number">2</span>
<span class="number">3</span> <span class="number">1</span>
<span class="identifier">The</span> <span class="identifier">solution</span> <span class="identifier">is</span><span class="special">:</span>
<span class="special">(</span><span class="number">0.6</span><span class="special">,-</span><span class="number">0.6</span><span class="special">)</span>   <span class="special">(</span><span class="number">0.7</span><span class="special">,-</span><span class="number">0.7</span><span class="special">)</span>
<span class="special">(</span><span class="number">0.64</span><span class="special">,-</span><span class="number">0.68</span><span class="special">)</span> <span class="special">(</span><span class="number">0.58</span><span class="special">,-</span><span class="number">0.46</span><span class="special">)</span>
<span class="identifier">The</span> <span class="identifier">relative</span> <span class="identifier">error</span> <span class="identifier">is</span><span class="special">:</span> <span class="number">2.63132e-34</span>
</pre>
</div>
<div class="copyright-footer">Copyright © 2002-2020 John
      Maddock and Christopher Kormanyos<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="hash.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tut.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="new_backend.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
